﻿using System;
using Service4u2.Json;
using System.Collections.Generic;
using Service4u2.Common;
using System.Net;
using System.Xml.Linq;

namespace MoAds.AdAdapters.ZestAdz
{

    public class ZestAdzService
    {
        private readonly String uri = "http://a.zestadz.com/multiple_ad";
        /// <summary>
        /// Occurs when a result is received.
        /// </summary>
        public event EventHandler<EventArgs<ZestAdzResponse>> ResultEvent;

        /// <summary>
        /// Occurs when an exception occurs..
        /// </summary>
        public event EventHandler<EventArgs<Exception>> ExceptionEvent;

        // This method is used to send request to  zestadz server.
        public void BeginAdsAsync(ZestAdzRequest request)
        {
            //  Creating Web client object 
            WebClient zestadzClient = new WebClient();

            zestadzClient.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(zestadzClient_DownloadStringCompleted);

            UriBuilder ub = new UriBuilder(uri);
            ub.Query = request.GetQueryString();

            zestadzClient.DownloadStringAsync(ub.Uri);
            
        }

        private void zestadzClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

            // if error, return
            if (e.Error != null)
            {
                if (ExceptionEvent != null) ExceptionEvent(this, new EventArgs<Exception>() { Argument = e.Error });
                return;
            }

            XElement xmlads = XElement.Parse(e.Result);

            ZestAdzResponse response = null;

            // Checking Null value.
            if (xmlads.Descendants("ad") != null)
            {
                foreach (var item in xmlads.Descendants("ad"))
                {
                    try
                    {
                        response = new ZestAdzResponse();

                        response.Url = item.Element("url").Value;

                        switch (item.Attribute("type").Value)
                        {
                            case "text":
                                response.Text = item.Element("text").Value;
                                break;

                            case "picture":
                                response.Picture = item.Element("picture").Value;
                                break;

                            default: //error message is in the url element
                                if (ExceptionEvent != null) ExceptionEvent(this, new EventArgs<Exception>() { Argument = new Exception(item.Element("error").Value) });
                                break;
                        }

                        if (ResultEvent != null) ResultEvent(this, new EventArgs<ZestAdzResponse>() { Argument = response });
                    }
                    catch (Exception ex)
                    {
                        if (ExceptionEvent != null) ExceptionEvent(this, new EventArgs<Exception>() { Argument = ex });
                    }
                }
            }
        }
    }
}
